import SwiftUI
import SwiftData

// TabPageContainer
// Tab界面分发

struct TabPageContainer: View {
    @Environment(\.modelContext) private var modelContext
    @Query private var loginInfo: [LoginInfo]
    @Query private var userInfo: [UserInfo]
    
    enum TabViewSelection {
        case look
        case sell
        case me
    }
    @State private var tabViewSelection: TabViewSelection = .sell

    var body: some View {
        TabView(selection: $tabViewSelection) {
            LookPage()
                .tabItem {
                    Label("看一看", systemImage: "book.pages")
                }
                .tag(TabViewSelection.look)
                .onAppear(perform: UpdateUserInfo)
            
            SellPage()
                .tabItem {
                    Label("转一转", systemImage: "plus")
                }
                .tag(TabViewSelection.sell)
                .onAppear(perform: UpdateUserInfo)
            
            MePage()
                .tabItem {
                    Label("我", systemImage: "person")
                    
                }
                .tag(TabViewSelection.me)
                .onAppear(perform: UpdateUserInfo)
        }
    }
    
    func UpdateUserInfo() {
        RequestUserInfo(modelContext: modelContext, userId: loginInfo.last?.userId ?? "")
    }
}

#Preview {
    TabPageContainer()
}
